package com.fatgirl.utils.binding
{
	import flash.events.EventDispatcher;

	/**
	 * 基于事件机制的元数据 
	 * @author iceman
	 * 
	 */
	public class MetaData extends EventDispatcher
	{
		private var m_ListenerList:Array;
		private var m_EventsVector:Vector.<EventListenerItem> = new Vector.<EventListenerItem>;
		public function MetaData()
		{
		}
		public function update(_pro:String,_value:Object):void
		{
			//if(this[_pro] == _value) return; 
			if(this[_pro] is Function) (this[_pro] as Function).call(null,_value); else this[_pro] = _value;
			var t_e:BindingEvent = new BindingEvent(_pro);
			t_e.value = _value;
			this.dispatchEvent(t_e);
		}
		public function getValue(_pro:String):Object
		{
			if(this[_pro] is Function)
			{
				return (this[_pro] as Function).call(null);
			}
			return this[_pro];
		}
		//准备销毁时使用
		public override function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type,listener,useCapture,priority,useWeakReference);
			m_EventsVector.push(new EventListenerItem(type,listener,useCapture));
		}
		public override function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			super.removeEventListener(type,listener,useCapture);
			for(var i:int=0;i<m_EventsVector.length;i++)
			{
				if(m_EventsVector[i].type==type && m_EventsVector[i].listener==listener && m_EventsVector[i].useCapture==useCapture)
				{
					m_EventsVector.splice(i,1);
					break;
				}
			}
		}
		/**
		 * 销毁此对象 包含此对象的所有监听 
		 * 
		 */
		public final function destroyAllListener():void
		{
			for(var i:int=0;i<m_EventsVector.length;i++) if(this.hasEventListener(m_EventsVector[i].type)) this.removeEventListener(m_EventsVector[i].type,m_EventsVector[i].listener,m_EventsVector[i].useCapture);
		}
	}
}
class EventListenerItem
{
	private var m_type:String;
	private var m_listener:Function;
	private var m_useCapture:Boolean;
	public function EventListenerItem(_type:String,_listener:Function,_useCapture:Boolean)
	{
		type = _type;
		listener = _listener;
		useCapture = _useCapture;
		
	}
	public function get useCapture():Boolean
	{
		return m_useCapture;
	}
	
	public function set useCapture(value:Boolean):void
	{
		m_useCapture = value;
	}
	public function get listener():Function
	{
		return m_listener;
	}
	
	public function set listener(value:Function):void
	{
		m_listener = value;
	}
	
	public function get type():String
	{
		return m_type;
	}
	
	public function set type(value:String):void
	{
		m_type = value;
	}
}